package com.lv.common.utils

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import java.io.ByteArrayOutputStream

/**
 * 作者： ygx
 * 创建日期：2023/2/11 2:49 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class YgxBitmapUtilsKt {

    companion object {

        //将drawable转换成可以用来存储的byte[]类型
        fun getPictureToByte(drawable: Drawable?): ByteArray? {
            if (drawable == null) {
                return null
            }
            val bd = drawable as BitmapDrawable
            val bitmap = bd.bitmap
            val os = ByteArrayOutputStream()
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os)
            return os.toByteArray()
        }

        fun byteToBitmap(byte: ByteArray): Drawable {
            val bitmap = BitmapFactory.decodeByteArray(byte, 0, byte.size, null)
            val bitmapDrawable = BitmapDrawable(bitmap)
            return bitmapDrawable
        }
    }


}


/**
 * 从图片地址获取Bitmap
 * @receiver Context
 * @param url String
 * @return Bitmap?
 */
//suspend fun Context.getImageBitmapByUrl(url: String): Bitmap? {
//    val request = ImageRequest.Builder(this)
//        .data(url)
//        .allowHardware(false)
//        .build()
//    val result = (imageLoader.execute(request) as SuccessResult).drawable
//    return (result as BitmapDrawable).bitmap
//}
